import { NodeStatus } from "../../../common/Enum";
import BTComposite from "../../Base/BTComposite";
/** 选择节点，其中一个子节点返回true就返回true，所有节点返回false就返回false */
export default class CompositeSelector extends BTComposite {
  
    onStart(): void {
        super.onStart()
        this.index = 0
    }

    
    canExecute(): boolean {
        return this.index < this.children.length && this.status !== NodeStatus.Success
    }

    onChildExecuted(status: NodeStatus): void {
        switch (status) {
            case NodeStatus.Success:
                this.status = NodeStatus.Success
                break;
            case NodeStatus.Failure:
                this.index++
                if (this.index >= this.children.length) {
                    this.status = NodeStatus.Failure
                } else {
                    this.status = NodeStatus.Running
                }
                break;
            case NodeStatus.Running:
                this.status = NodeStatus.Running
                break;
            default:
                break;
        }
    }

    onConditionalAbort(index: number) {
        this.index = index
        this.status = NodeStatus.Inactive
    }
    // onUpdate(): NodeStatus {

    //     if (this.status === NodeStatus.Success) {
    //         return NodeStatus.Success
    //     }

    //     if (this.index >= this.children.length) {
    //         this.status = NodeStatus.Failure
    //         return NodeStatus.Failure
    //     }

    //     const child = this.children[this.index]
    //     const res = child.run()

    //     if (res === NodeStatus.Success) {
    //         this.status = NodeStatus.Success
    //         return NodeStatus.Success
    //     }

    //     if (res == NodeStatus.Failure) {
    //         this.index++
    //     }

    //     return NodeStatus.Running
    // }

}